﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using METU_CMT.Business;

namespace METU_CMT
{
    public partial class EditProjectTeamScreen : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindAll();
            }
        }

        private void BindTeams()
        {
            ddlTeams.DataSource = EntityFactory.GetCurrentCourse(UserType.Instructor).Teams;
            ddlTeams.DataBind();
        }

        protected void OnLnkBtnEditTeamsClicked(object sender, EventArgs e)
        {
            Response.Redirect("~/EditTeamsScreen.aspx");
        }

        private void BindStudents()
        {
            lstStudents.DataSource = ((CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor)).GetUnassignedStudents();
            lstStudents.DataBind();
        }

        protected void OnBtnAddClicked(object sender, EventArgs e)
        {
            if(ddlTeams.SelectedValue == null)
            {
                MsgBox1.alert("Please select a team!");
                return;
            }

            if(ddlTeams.SelectedItem == null)
            {
                MsgBox1.alert("Please select a team!");
                return;
            }
            if (lstStudents.SelectedItem == null)
            {
                MsgBox1.alert("Please select a student!");
                return;
            }

            int _teamId = ddlTeams.GetSelectedValue();
            int _studentId = lstStudents.GetSelectedValue();

            Student _student = new Student(_studentId);

            CourseInstructor _currentCourse = ( CourseInstructor ) EntityFactory.GetCurrentCourse(UserType.Instructor);
            _currentCourse.AddTeamMember( _teamId, _student );
            
            BindAll();
        }

        class LocalTeam
        {
            public int TeamId { get; set; }

            public string TeamName { get; set; }

            public string TeamMemberName { get; set; }

            public int StudentId { get; set; }
        }

        private void BindTeamMembers()
        {
            CourseInstructor _currentCourse = ( CourseInstructor ) EntityFactory.GetCurrentCourse(UserType.Instructor);
            List< LocalTeam > _localTeams = ( from _team in _currentCourse.Teams
                                              from _student in _team.Members
                                              select new LocalTeam
                                                         {
                                                             TeamId = _team.TeamId,
                                                             TeamName = _team.TeamName,
                                                             TeamMemberName = _student.Name + ' ' + _student.Surname,
                                                             StudentId = _student.UserId
                                                         } ).ToList( );

            dtgTeamDetails.DataSource = _localTeams;
            dtgTeamDetails.DataBind();
        }

        protected void OnDtgTeamDetailsRowCommand( object sender, GridViewCommandEventArgs e )
        {
            if (e.CommandName.CompareTo("RemoveAssignment") == 0)
            {
                int _teamId;
                if(int.TryParse( e.CommandArgument.ToString(  ), out _teamId ) == false)
                {
                  //TODO: Log: COuldnot get id;  
                    return;
                }

                Team _team = new Team( _teamId );
                int _studentId = CommonMethods.GetIdFromGridViewRow((GridViewRow)((LinkButton)e.CommandSource).NamingContainer);

                Student _student = new Student(_studentId);
                _team.DeleteMember( _student );

                BindAll( );
            }
        }

        private void BindAll( )
        {
            BindTeams(  );
            BindStudents(  );
            BindTeamMembers(  );
        }
    }
}